import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateReportDto } from './dto/create-report.dto';
import { Report } from './entities/report.entity';

@Injectable()
export class ReportsService {
  constructor(
    @InjectRepository(Report)
    private reportRepository: Repository<Report>,
  ) {}

  generate(createReportDto: CreateReportDto) {
    const report = this.reportRepository.create({
      ...createReportDto,
      status: 'completed',
      filePath: `/reports/${Date.now()}.${createReportDto.format || 'pdf'}`,
      fileSize: Math.floor(Math.random() * 1000000) + 100000, // 模拟文件大小
    });
    
    return this.reportRepository.save(report);
  }

  findAll() {
    return this.reportRepository.find({
      order: { createdAt: 'DESC' }
    });
  }

  async findOne(id: number) {
    const report = await this.reportRepository.findOne({ where: { id } });
    
    if (!report) {
      throw new NotFoundException(`Report with ID ${id} not found`);
    }
    
    return report;
  }

  async delete(id: number) {
    const report = await this.findOne(id);
    
    await this.reportRepository.remove(report);
    
    return { success: true, message: `Report ${id} has been deleted` };
  }
} 